package exmpl;


import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet(urlPatterns = "/servlet01",description = "第一个servlet",
        name = "servlet",asyncSupported = true,initParams = {})

public class Servlet extends HttpServlet {
    @Override
    public void destroy() {
        System.out.println("servlet被销毁");
        super.destroy();
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("servlet被初始化");
        super.init(config);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doget");
        //获取的servletconfig对象
        ServletConfig config = super.getServletConfig();
        //获取配置的servlet-name
        String name = config.getServletName();
        System.out.println(name);
        //获取配置的密码
        String password = config.getInitParameter("password");
        System.out.println(password);
        //获取所有配置的参数名称
        Enumeration<String> names = config.getInitParameterNames();
        while (names.hasMoreElements()){
            String prameter =  names.nextElement();
            //获取prameter对应的参数值
            String value = config.getInitParameter(prameter);
            System.out.println(prameter +":"+value);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("dopost");
    }
}
